﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using Lswweb.Model;
using Lswweb.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Lswweb.Common;

namespace Lswweb.Domain
{
    public class SiteSettings
    {
        #region 成员字段和构造方法

        private SiteContext siteContext = SiteContext.Current;

        private int siteSettingId = 1;
        private string _siteName = "lswweb";
        private string _siteDomain = "www.mx9.cn";
        private string _siteEmail = "lswweb@163.com";
        private string _sitePhone = string.Empty;
        private string _siteAddress=string.Empty;
        private string _siteFax=string.Empty;
        private string _sitePostalCode=string.Empty;
        private string _siteCopyRight=string.Empty;
        private string _siteTCP=string.Empty;
        private string _siteBusinesslicense = string.Empty;

        private string _dateFormat = "yyy-MM-dd";
        private string _timeFormat= "hh:mm";

        private string _smtpEmail = string.Empty;
        private string _smtpServer = string.Empty;
        private string _smtpUser = string.Empty;
        private string _smptPsw = string.Empty;

        private string _upDirectory = "/UpFiles/";
        private string _fckeditorFileUploadPath = "/UpFiles/FCKeditor/";
        private int _upFileMaxSize = 100;
        private string _imageExtensions = "jpg|jpeg|gif||png";
        private int _thumbnailMaxSize = 100;
        private bool _waterMark = false;
        private string _waterMarkText = string.Empty;

        private int _verifyCodeLength = 5;

        private string expressDeliveryExpressions = "15+[(w-1000)/500]*5";

        private Hashtable settings;
        private ISiteSettings siteSettingsDAL;
        private string ver = string.Empty;

        public SiteSettings(string language)
        {
            //从数据库或配置文件读取配置信息
            this.ver = language;
            siteSettingsDAL = DALFactory.CreateSiteSettings();
            settings = siteSettingsDAL.GetSettings(siteSettingId, ver);
            if (settings.Count == 0)
            {
                foreach (KeyValuePair<string, string> setting in ResourceManagement.Content.Settings)
                {
                    this.settings.Add(setting.Key, setting.Value);
                }
            }
        }

        #endregion

        #region 静态公共方法

        private static IDictionary<string,SiteSettings> _settings;
        private static object lockThis = new object();
        public static SiteSettings Settings
        {
            get
            {
                if (_settings == null)
                    _settings = new Dictionary<string, SiteSettings>();

                string currentLanguage = SiteContext.Current.Language;
                if (!_settings.ContainsKey(currentLanguage))
                {
                    lock (lockThis)
                    {
                        if (!_settings.ContainsKey(currentLanguage))
                        {
                            _settings.Add(currentLanguage, new SiteSettings(currentLanguage));
                        }
                    }
                }

                return _settings[currentLanguage];
            }
        }

        /// <summary>
        /// 根据模块，日期生成上传目录
        /// </summary>
        /// <param name="siteModule"></param>
        /// <returns>生成类似 /News/2008-10/10/的目录url</returns>
        public static string GetUploadDirectory(SiteModule siteModule)
        {
            return UrlHelper.ResolveUrl(Settings.UploadDirectory + siteModule.ToString() + "/" + DateTime.Now.Year + "-" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
        }

        #endregion

        #region 保存设置

        public void Save()
        {
            this.siteSettingsDAL.Insert(this.siteSettingId, this.settings, this.ver);
        }

        #endregion

        private T GetSetting<T>(string key, T defaultValue)
        {
            key = key.ToLower();
            if (settings.ContainsKey(key))
                return (T)Convert.ChangeType(settings[key],typeof(T));
            
            return defaultValue;
        }
        private void SetSetting(string key, object value)
        {
            key = key.ToLower();
            if (settings.ContainsKey(key))
                settings.Remove(key);

            settings.Add(key,value);
        }

        #region 站点信息

        public string SiteName
        {
            get { return GetSetting<string>("siteName", this._siteName); }
            set { SetSetting("siteName",value); }
        }

        public string SiteDomain
        {
            get { return GetSetting<string>("siteDomain", this._siteDomain); }
            set { SetSetting("siteDomain",value); }
        }

        public string SiteEmail
        {
            get { return GetSetting<string>("siteEmail", this._siteEmail); }
            set { SetSetting("siteEmail",value); }
        }

        public string SitePhone
        {
            get { return GetSetting<string>("sitePhone", this._sitePhone); }
            set { SetSetting("sitePhone",value); }
        }

        public string SiteAddress
        {
            get { return GetSetting<string>("siteAddress", this._siteAddress); }
            set { SetSetting("siteAddress",value); }
        }

        public string SiteFax
        {
            get { return GetSetting<string>("siteFax", this._siteFax); }
            set { SetSetting("siteFax",value); }
        }

        public string SitePostalCode
        {
            get { return GetSetting<string>("sitePostalCode", this._sitePostalCode); }
            set { SetSetting("sitePostalCode",value); }
        }

        public string SiteCopyRight
        {
            get { return GetSetting<string>("siteCopyRight", this._siteCopyRight); }
            set { SetSetting("siteCopyRight",value); }
        }

        public string SiteICP
        {
            get { return GetSetting<string>("siteTCP", this._siteTCP); }
            set { SetSetting("siteTCP",value); }
        }

        public string SiteBusinesslicense
        {
            get { return GetSetting<string>("siteBusinesslicense", this._siteBusinesslicense); }
            set { SetSetting("siteBusinesslicense",value); }
        }

        public string[] SiteQQ
        {
            get { return GetSetting<string>("siteQQ", string.Empty).Split(','); }
            set { SetSetting("siteQQ", value.CombinationToString(',')); }
        }

        public string[] SiteMSN
        {
            get { return GetSetting<string>("siteMSN", string.Empty).Split(','); }
            set { SetSetting("siteMSN",value.CombinationToString(',')); }
        }

        #region 页面Meta 标签属性

        //added by jacky
        //2004-11-19
        //增加页面的Meta标签：Keywords和Description属性
        /// <summary>
        /// 页面Meta标记的Keywords属性
        /// </summary>
        public string MetaKeywords
        {
            get { return GetSetting<string>("metaKeywords", string.Empty); }
            set { SetSetting("metaKeywords",value); }
        }

        /// <summary>
        /// 页面Meta标记的Description属性
        /// </summary>
        public string MetaDescription
        {
            get { return GetSetting<string>("metaDescription", string.Empty); }
            set { SetSetting("metaDescription",value); }
        }

        #endregion

        public string CompanyName
        {
            get { return GetSetting<string>("companyName", string.Empty); }
            set { SetSetting("companyName",value); }
        }

        public string SiteMobile
        {
            get { return GetSetting<string>("siteMobile", string.Empty); }
            set { SetSetting("siteMobile",value); }
        }

        public string LinkMan
        {
            get { return GetSetting<string>("linkMan", string.Empty); }
            set { SetSetting("linkMan", value); }
        }

        #endregion

        #region 格式化配置

        public string DateTimeFormat
        {
            get
            {
                return DateFormat + " " + TimeFormat;
            }
        }

        public string DateFormat
        {
            get { return GetSetting<string>("dateFormat", this._dateFormat); }
            set { SetSetting("dateFormat",value); }
        }

        public string TimeFormat
        {
            get { return GetSetting<string>("timeFormat", this._timeFormat); }
            set { SetSetting("timeFormat",value); }
        }

        #endregion

        #region other

        public int VerifyCodeLength
        {
            get { return GetSetting<int>("verifyCodeLength", this._verifyCodeLength); }
            set { SetSetting("verifyCodeLength",value); }
        }

        public string Statistic
        {
            get { return GetSetting<string>("Statistic", string.Empty); }
            set { SetSetting("Statistic",value); }
        }

        public string[] SearchKeys
        {
            get { return GetSetting<string>("HotSearchKeys", string.Empty).Split(','); }
            set { SetSetting("HotSearchKeys", value.CombinationToString(',')); }
        }

        public decimal VipIntegral
        {
            get { return GetSetting<decimal>("vipIntegral", 5000); }
            set { SetSetting("VipIntegral", value); }
        }

        public decimal LogisticsInsurance
        {
            get { return GetSetting<decimal>("LogisticsInsurance", (decimal)2.98); }
            set { SetSetting("LogisticsInsurance", value); }
        }

        public decimal VipDiscount
        {
            get { return GetSetting<decimal>("VipDiscount", 98); }
            set { SetSetting("VipDiscount", value); }
        }

        

        #endregion

        #region Payment

        public string PaymentUser
        {
            get { return GetSetting<string>("PaymentUser", string.Empty); }
            set { SetSetting("PaymentUser", value); }
        }

        public string Partner
        {
            get { return GetSetting<string>("Partner", string.Empty); }
            set { SetSetting("Partner", value); }
        }

        public string PaymentKey
        {
            get { return GetSetting<string>("PaymentKey", string.Empty); }
            set { SetSetting("PaymentKey", value); }
        }

        public string PaymentPaypalUserName
        {
            get { return GetSetting<string>("PaymentPaypalUserName", string.Empty); }
            set { SetSetting("PaymentPaypalUserName", value); }
        }

        #endregion

        #region SMTP 配置

        public string SmtpServer
        {
            get { return GetSetting<string>("smtpServer", this._smtpServer); }
            set { SetSetting("smtpServer",value); }
        }

        public string SmtpEmail
        {
            get { return GetSetting<string>("smtpEmail", this._smtpEmail); }
            set { SetSetting("smtpEmail",value); }
        }

        public string SmtpUser
        {
            get { return GetSetting<string>("smtpUser", this._smtpUser); }
            set { SetSetting("smtpUser",value); }
        }

        public string SmptPsw
        {
            get { return GetSetting<string>("smptPsw", this._smptPsw); }
            set { SetSetting("smptPsw",value); }
        }

        #endregion

        #region 文件上传配置

        /// <summary>
        /// 上传文件根目录
        /// </summary>
        public string UploadDirectory
        {
            get { return GetSetting<string>("upDirectory", this._upDirectory); }
            set { SetSetting("upDirectory",value); }
        }

        /// <summary>
        /// 在fckeditor的config.ascx文件中设置
        /// </summary>
        public string FCKeditorUploadFileDirectory
        {
            get { return GetSetting<string>("FCKeditor:UserFilesPath", this._fckeditorFileUploadPath); }
            set { SetSetting("FCKeditor:UserFilesPath",value); }
        }

        public string UploadTempDirectory
        {
            get { return UploadDirectory + "Temp/"; }
        }

        /// <summary>
        /// 上传文件限制大小
        /// </summary>
        public int UpFileMaxSize
        {
            get { return GetSetting<int>("upFileMaxSize", this._upFileMaxSize); }
            set { SetSetting("upFileMaxSize",value); }
        }

        /// <summary>
        /// 上传图片所充许的扩展名
        /// </summary>
        public string ImageExtensions
        {
            get { return GetSetting<string>("imageExtensions", this._imageExtensions); }
            set { SetSetting("imageExtensions",value); }
        }

        /// <summary>
        /// 缩略图大小
        /// </summary>
        public int ThumbnailMaxSize
        {
            get { return GetSetting<int>("thumbnailMaxSize", this._thumbnailMaxSize); }
            set { SetSetting("thumbnailMaxSize",value); }
        }

        public bool WaterMark
        {
            get { return GetSetting<bool>("waterMark", this._waterMark); }
            set { SetSetting("waterMark",value); }
        }

        public string WaterMarkText
        {
            get { return GetSetting<string>("waterMarkText", this._waterMarkText); }
            set { SetSetting("waterMarkText",value); }
        }

        #endregion

        public string ExpressDeliveryExpressions
        {
            get { return GetSetting<string>("expressDeliveryExpressions", this.expressDeliveryExpressions); }
            set { SetSetting("expressDeliveryExpressions", value); }
        }
    }
}
